package com.jt.project;

import com.jt.datasource.JedisDataSource;
import redis.clients.jedis.Jedis;

import java.util.Set;

public class VoteDemo01 {
    static boolean doVote(String activityId,String userId){

        Jedis jedis= JedisDataSource.getConnection();
        Boolean flag=jedis.sismember(activityId,userId);
        if (flag){
            jedis.srem(activityId,userId);
            flag=false;
        }else {
            jedis.sadd(activityId,userId);
            flag=true;
        }
        jedis.close();
        return flag;
    }
    static Long doCount(String activityId){
        Jedis jedis=JedisDataSource.getConnection();
        Long count = jedis.scard(activityId);
        jedis.close();
        return count;
    }
    static Set<String> doGetUsers(String activityId){
        Jedis jedis = JedisDataSource.getConnection();
        Set<String> members = jedis.smembers(activityId);
        jedis.close();
        return members;
    }
    public static void main(String[] args) {
        //1.定义活动(计提春游活动)-用户活动id做标识
        String activityId="101";
        //2.定义参与活动的用户
        String user1="2001";
        String user2="2002";
        String user3="2003";
        //3.执行用户投票操作
        boolean flag1=doVote(activityId,user1);
        boolean flag2=doVote(activityId,user2);
        boolean flag3=doVote(activityId,user3);
        boolean flag4=doVote(activityId,user1);
//        //4.查看投票总数
        long count=doCount(activityId);
        System.out.println(activityId+"总投票数："+count);
//        //5.查看那些用户参与了投票
        Set<String> userIds=doGetUsers(activityId);
        System.out.println("参与这个活动的用户为:"+count);
    }
}
